package per.wxp.kafka.service;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import per.wxp.kafka.entity.Message;

@Service
public class KafkaService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private KafkaTemplate kafkaTemplate;
    private Gson gson = new GsonBuilder().create();

    public Boolean sendKafka(String topic,Message m){

        boolean b=false;
        try {
            logger.info("发送kafka={}", m.toString());
            kafkaTemplate.send(topic, gson.toJson(m));
            b=true;
        }catch (Exception e){
            logger.error("发送kafka失败", e);
        }
        return b;
    }

}
